package com.instalinks.android.apps.service;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;

import com.instalinks.android.apps.HomeActivity;
import com.instalinks.android.apps.helper.AppConfig;
import com.instalinks.android.apps.receiver.SmsReceiver;
import com.instalinks.android.lib.AppHelper;
import com.instalinks.android.lib.ApplicationNotifications;
import com.instalinks.android.lib.enums.TimeOuts;

public class AppService extends Service {

	// private AppConfig mAppConfig;
	private ApplicationNotifications appNotifications;
	private Handler wakeupHandler;
	private AppConfig mAppConfig;
	
//	private IntentBroadcast intentBroadcast;
	
	private SmsReceiver mSmsReceiver;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		mAppConfig = new AppConfig(getApplicationContext());
		appNotifications = mAppConfig.getServiceNotificationInstance(false);

		registerHandlers();

		mSmsReceiver = new SmsReceiver();
		IntentFilter actionFilter = new IntentFilter(com.instalinks.android.lib.LibConstants.IntentAction.SMS_RECEIVER_ANDROID);
		this.registerReceiver(this.mSmsReceiver, actionFilter);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);

		appNotifications.notify("Instalinks Apps Service Enabled",
				HomeActivity.class.getName());

		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		appNotifications = mAppConfig.getServiceNotificationInstance(false);
		appNotifications.notify("Instalinks Apps Service Disabled",
				HomeActivity.class.getName());

		try {
			wakeupHandler.removeCallbacks(wakeUp);
		} catch (Exception e) {
		}
		
//		try {
//			intentBroadcast.removeAll();
//		} catch (Exception e) {
//		}
		
		try {
			this.unregisterReceiver(this.mSmsReceiver);
		} catch (Exception e) {
		}
	}

	
	private void registerHandlers(){
		wakeupHandler = registerHandler(wakeUp);
		
//		intentBroadcast = new IntentBroadcast(getApplicationContext());
		
//		intentBroadcast.addHandler(LibConstants.IntentAction.ALERT_05MIN_BROADCAST, TimeOuts.FiveMin, "timer_broadcast","5min");
//		intentBroadcast.addHandler(LibConstants.IntentAction.ALERT_15MIN_BROADCAST, TimeOuts.FifteenMin, "timer_broadcast","15min");
//		intentBroadcast.addHandler(LibConstants.IntentAction.ALERT_30MIN_BROADCAST, TimeOuts.ThirtyMin, "timer_broadcast","30min");
//		intentBroadcast.addHandler(LibConstants.IntentAction.ALERT_60MIN_BROADCAST, TimeOuts.OneHr, "timer_broadcast","60min");
//		intentBroadcast.addHandler(LibConstants.IntentAction.ALERT_3HR_BROADCAST, TimeOuts.ThreeHr, "timer_broadcast","3hr");
//		intentBroadcast.addHandler(LibConstants.IntentAction.ALERT_6HR_BROADCAST, TimeOuts.SixHr, "timer_broadcast","6hr");
		
	}
	
	private Handler registerHandler(Runnable r){
		Handler h = new Handler();
		h.removeCallbacks(r);
		h.postAtTime(r, TimeOuts.OneSec.getValue());
		
		return h;
	}
	
	private Runnable wakeUp = new Runnable() {

		@Override
		public void run() {
			AppHelper.wakeUp(getApplicationContext(), 1000);
			wakeupHandler.postDelayed(this, TimeOuts.FifteenMin.getValue());
		}
	};
}
